package com.atguigu.tingshu.album.service;


import java.io.File;

public class TTSUtils {
    public static void main(String[] args) {
        easy("第一章 牢狱之灾 \n" +
             "许七安幽幽醒来，嗅到了空气中潮湿的腐臭味，令人轻微的不适，胃酸翻涌。\n" +
             "    这扑面而来的臭味是怎么回事，家里的二哈又跑床上拉屎来了....\n" +
             "   根据熏人程度，怕不是在我头顶拉的....\n" +
             "    许七安家里养了一条狗，品种哈士奇，俗称二哈。",
             "3-dfdgr.mp3" ,
             //                "zh-TW-YunJheNeural"
             "zh-CN-YunyangNeural"
             //                "zh-HK-WanLungNeural"
            );
    }
    public static void easy(String txt,String fileName , String soundType) {

        String ttsPath = "C:/Users/10055/AppData/Roaming/Python/Python313/Scripts/edge-tts.exe";
//        String ttsPath = "edge-tts";
        // 保存地址
        String saveFilePath = "E://"+fileName;
        // 选择声音模型
        //        soundType = "zh-CN-XiaoyiNeural";
        try {
            //先创建文件，防止写入数据卡住
            File file = new File(saveFilePath);
            file.createNewFile();
            ProcessBuilder processBuilder = new ProcessBuilder(
                ttsPath,
                "--voice", soundType,
                "--text", txt,
                "--write-media",saveFilePath );

            Process process = processBuilder.start();
            // 等待FFmpeg进程完成
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("音频转换成功!");
                // 音频的具体信息
                //                SoundInfo.getAudioInfo(saveFilePath);
            } else {
                System.out.println("音频转换失败!");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}